package cn.bigfire.wedesk.net.initializer;

import cn.bigfire.wedesk.net.codec.ProtocolWebSocketDecoder;
import cn.bigfire.wedesk.net.codec.ProtocolWebSocketEncoder;
import cn.bigfire.wedesk.net.handler.ServerHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

public class WebSocketChannelInitializer extends ChannelInitializer<NioSocketChannel> {

    @Override
    protected void initChannel(NioSocketChannel channel) throws Exception {
        channel.pipeline().addLast(new HttpServerCodec());
        channel.pipeline().addLast(new ChunkedWriteHandler());
        channel.pipeline().addLast(new HttpObjectAggregator(65535));
        channel.pipeline().addLast(new WebSocketServerProtocolHandler("/wedesk/websocket"));
        channel.pipeline().addLast(new ProtocolWebSocketEncoder());
        channel.pipeline().addLast(new ProtocolWebSocketDecoder());
        channel.pipeline().addLast(new ServerHandler());
    }

}
